var myScroll = [];	//scroll 관리
var debug;

$(document).delegate('[data-role="page"]', 'pageshow', function () {
    var $page = $(this); 
	var pageId = $page.attr("id");
 
    // setup iScroll 
    $($page.find('.wrapper')).each( function(index) {
        var scroller_id = $(this).get(0);
        myScroll.push ( 
        	new iScroll(scroller_id, {
				onBeforeScrollStart: function (e) {
					var target = e.target;

					while (target.nodeType != 1) target = target.parentNode;
								
					if( target.tagName != 'SELECT' && target.tagName != 'INPUT' && target.tagName != 'TEXTAREA' )
						e.preventDefault();
					}
        		 }
        	 )
        );
    });
	// 메인 탭 뷰만 footer가 보여짐
	if( pageId == "pageMainAga" || pageId=="pageMainSns" || pageId=="pageMainDiary" || pageId=="pageMainProfile" || pageId=="pageMainMore" ){
		$("#footer").show();
		$("#sub_wrapper").css("bottom","76px");
	}else{
		$("#footer").hide();
		$("#sub_wrapper").css("bottom","0px");
	}
	
	/*main_aga.js에 init함수 정의*/
	if( pageId == "pageMainAga"){	
		initPageMainAga();
	}else if( pageId == "pageMainSns"){
		initPageMainSns();
	}else if( pageId == "pageMainDiary"){
		initPageMainDiary();
	}else if( pageId == "pageMainProfile"){
		initPageMainProfile();
	}else if ( pageId == "pageMainMore" ){
		initPageMainMore();
	}
	/*diary.js에 init함수 정의*/
	else if( pageId == "pageDiaryGrowth"){
		initPageDiaryGrowth();
	}else if( pageId == "pageDiarySleep" ){
		initPageDiarySleep();
	}else if( pageId == "pageDiarySleeping" ){
		initPageDiarySleeping();
	}else if( pageId == "pageDiaryWakeUp" ){
		initPageDiaryWakeUp();
	}else if( pageId == "pageDiaryBath"){
		initPageDiaryBath();
	}else if ( pageId == "pageDiaryHeight"){
		initPageDiaryHeight();
	}else if ( pageId == "pageDiaryWeight"){
		initPageDiaryWeight();
	}else if ( pageId == "pageDiaryPoo" ){
		initPageDiaryPoo();
	}else if ( pageId == "pageDiaryTeeth"){
		initPageDiaryTeeth();
	}else if ( pageId == "pageDiaryEat"){
		initPageDiaryEat();
	}else if ( pageId == "pageDiaryPhoto"){
		initPageDiaryPhoto();
	}else if ( pageId == "pageDiaryVideo"){
		initPageDiaryVideo();
	}else if( pageId == "pageDiaryGrowthDetail"){
		initPageDiaryGrowthDetail();
	}else if( pageId == "pageDiaryPost"){
		initPageDiaryPost();
	}
	/*profile.js에 init함수 정의*/
	else if( pageId == "pageProfileFriendlist"){
		initPageProfileFriendlist();
	}else if( pageId == "pageProfileInviteAga"){
		initPageProfileInviteAga();
	}else if( pageId == "pageProfileFamilylist"){
		initPageProfileFamilylist();
	}else if( pageId == "pageProfileInviteFamily"){
		initPageProfileInviteFamily();
	}else if( pageId == "pageProfileAgalist"){
		initPageProfileAgalist();
	}else if ( pageId == "pageCreateAga" ){
		initPageCreateAga();
	}else if ( pageId == "pageModifyProfile" ){
		initPageModifyProfile();
	}
	/*sns.js에 init함수 정의*/
	else if( pageId == "pageRecommend"){
		initPageRecommend();
	}else if ( pageId == "pageMessageBox" ){
		initPageMessageBox();
	}else if ( pageId == "pageMessage" ){
		initPageMessage();
	}else if ( pageId == "pageSendMessage" ){
		initPageSendMessage();
	}else if ( pageId == "pageNews" ){
		initPageNews();
	}else if ( pageId == "pageReply"){
		initPageReply();
	}

	for (x in myScroll) 
		myScroll[x].refresh();
}); 

function initPageMainAga()
{
	nowURL = "main_aga.html";

	$("#header").html(postInfo.babyNick + "'s 방<p id='btnHeaderNew' class='btnHeaderNew btnHeaderCenter'></p>");//<p id='btnSort' class='btnHeaderSort btnHeaderRight'></p>");
	new Quickly('btnHeaderNew', function(e){
		$.mobile.changePage("sns/news.html",{transition:'slide'})
	});
	
	new Quickly('btn_reload', function(e){
		requestPostToServer( user.id, postInfo.babyId, postInfo.startIndex );
	})
	// 한포스트를 보고 다시 refresh하는 경우(예, 댓글단 경우)
	if( postInfo.selectedPostId != -1 ){
		requestDetailPostToServer( user.id, postInfo.selectedPostId, 'babyroom' );
	}else{
		requestPostToServer( user.id, postInfo.babyId, postInfo.startIndex );
	}
}

function initPageMainSns()
{
	nowURL = "main_sns.html";
	
	$("#header").html("<p id='btnHeaderRecommend' class='btnHeaderRecommend btnHeaderLeft'></p>친구만나기<p id='btnHeaderNew' class='btnHeaderNew btnHeaderCenter'></p>");//<p id='btnSort' class='btnHeaderSort btnHeaderRight'></p>");
	new Quickly('btnHeaderRecommend', function(e){
		$.mobile.changePage("sns/recommend.html",{transition:'slide'});
	});
	new Quickly('btnHeaderNew', function(e){
		$.mobile.changePage("sns/news.html",{transition:'slide'})
	});
	new Quickly('snsBtn_reload', function(e){
		requestSnsPostToServer( user.id, user.mainbaby_info.babyid, snsPostInfo.startIndex );
	})
	// 한포스트를 보고 다시 refresh하는 경우(예, 댓글단 경우)
	if( snsPostInfo.selectedPostId != -1 ){
		requestDetailPostToServer( user.id, snsPostInfo.selectedPostId, 'sns' );
	}else{
		requestSnsPostToServer( user.id, user.mainbaby_info.babyid, snsPostInfo.startIndex );
	}
}

function initPageMainDiary()
{
	nowURL = "main_diary.html";

	$("#header").html("아가일기");
	new Quickly('btnGrowth', function(e){
		$.mobile.changePage("diary/diary_growth.html", {transition : 'slide'});
	});
	$("#btnSleep").click(function(){
		var tempSleep = localStorage.getItem("sleepStart");
		if(  tempSleep == "false" || tempSleep == null )
			$.mobile.changePage("diary/diary_sleep.html",{transition:'slide'})
		else if( tempSleep == "true" )
			$.mobile.changePage("diary/diary_sleeping.html",{transition:'slide'})
	});
	$("#btnEat").click(function(){
		$.mobile.changePage("diary/diary_eat.html",{transition:'slide'});
	});
	$("#btnBath").click(function(){
		$.mobile.changePage("diary/diary_bath.html",{transition:'slide'});
	});
	$("#btnHeight").click(function(){
		$.mobile.changePage("diary/diary_height.html",{transition:'slide'});
	});
	$("#btnWeight").click(function(){
		$.mobile.changePage("diary/diary_weight.html",{transition:'slide'});
	});
	$("#btnPoo").click(function(){
		$.mobile.changePage("diary/diary_poo.html",{transition:'slide'});
	});
	$("#btnTeeth").click(function(){
		$.mobile.changePage("diary/diary_teeth.html",{transition:'slide'});
	});
	$("#btnPhoto").click(function(){
		$.mobile.changePage("diary/photo.html",{transition:'slide'});
	});
	$("#btnVideo").click(function(){
		$.mobile.changePage("diary/video.html",{transition:'slide'});
	});
}

function initPageMainProfile()
{
	nowURL = "main_profile.html";

	$("#header").html("아가정보<p id='btnHeaderNew' class='btnHeaderNew btnHeaderCenter'></p>");
	new Quickly('btnHeaderNew', function(){
		$.mobile.changePage("sns/news.html",{transition:'slide'});
	});
	
	// 현재 profile을 보여주기 원하는 babyId를 인자로 넣는다.
	requestProfileToServer( user.id, profileInfo.babyId );
}



function initPageMainMore()
{
	$("#header").html("각종 정보");
	
	for(var i=1; i<18; i++)
	{
		$("#more_when"+i).append("( 예정일 : "+infoJusa(i)+" )");
	}
}

function dateTimeFormat( tempDateTime )
{
	var dateTimeArray = tempDateTime.split(' ');
	var tempDate = dateTimeArray[0];
	var tempTime = dateTimeArray[1];
	
	var dateArray = tempDate.split('-');
	var timeArray = tempTime.split(':');
	
	return dateArray[1] +"\/"+ dateArray[2] +"\/"+ dateArray[0] + " " + timeArray[0]+ ":" + timeArray[1]+ ":" + timeArray[2].slice(0,2);
}

function myDateFormat( tempDate )
{
	console.log( tempDate );
	var tempArray = tempDate.split('-');
	
	//console.log( itemTimeArray[1] + "\/" + itemTimeArray[2] + "\/" + itemTimeArray[0] );
	return new Date( tempArray[0], tempArray[1], tempArray[2] );
}

function infoJusa( index )
{
	//var birth = new Date( dateFormat( user.mainbaby_info.birthday ) );
	var birth = myDateFormat( user.mainbaby_info.birthday );
	
	var jusa = 0;
	
	if( index == 1 || index == 2 )
		jusa = birth.getTime() + 1000* 60 * 60 * 24 * 30;
	else if( index > 2 && index < 6 )
		jusa = birth.getTime() + 1000* 60 * 60 * 24 * 30 * 2;
	else if( index == 6 || index == 7 )
		jusa = birth.getTime() + 1000* 60 * 60 * 24 * 30 * 4;
	else if( index > 7 && index < 11 )
		jusa = birth.getTime() + 1000* 60 * 60 * 24 * 30 * 6;
	else if( index > 10 && index < 15 )
		jusa = birth.getTime() + 1000* 60 * 60 * 24 * 30 * 12;
	else if( index == 15 || index == 16 )
		jusa = birth.getTime() + 1000* 60 * 60 * 24 * 30 * 24;
	else if( index == 17 )
		jusa = birth.getTime() + 1000* 60 * 60 * 24 * 30 * 72;
	
	jusa = new Date( jusa );

	return jusa.format("isoDateTime").substr(0,10);
}

$(document).delegate('[data-role="page"]', 'pagehide', function () { 
    // unset and delete iScroll 
    for (x in myScroll) 
    { 
        myScroll[x].destroy(); 
        myScroll[x] = null; 
        myScroll.splice(x, 1); 
    } 
}); 



function refreshPost( postData, start )
{
	if( start == 0 )
		$("#postList").html("");
	
	$.each( postData, function(i, item){
		var itemTime = new Date( dateTimeFormat ( item.datetime ) );

		var temp = "<li id='detailPost"+(start+i)+"'>"
					+ "<input id='imgUrl"+(start+i)+"' style='display:none;' value='"+serverURL+'/photo/'+postInfo.babyId+'/post_'+item.photo.filename+"'/>"
					+ "<div id='img_bg"+(start+i)+"' class='img_bg'>"
					+ "<img id='main_profile_small_frame"+(start+i)+"' class='main_profile_small_frame' border='0' src='../images/transparency.png'/>"
					+ "<p id='main_title_text' class='main_title_text'>" + item.title + "</p>"
					+ "<p id='main_title_time' class='main_title_time'>" + dynamicDate(itemTime/1000) + "</p>"
					+ "<img id='postImage"+(start+i)+"' class='postImage' border='0' src='../images/transparency.png'/></div>"
					+ "<div id='reply_bg"+(start+i)+"' class='reply_bg'>"
					+ "<ul id='replyList"+(start+i)+"' class='replyList'>"
					//내용 부분 추가
					+ "<li id='replyListLi"+(start+i)+"_0' class='replyListLi'>"
					+ "<img id='reply_profile_small_frame"+(start+i)+"_0' class='reply_profile_small_frame' border='0' src='../images/transparency.png' />"
					+ "<p id='reply_name"+(start+i)+"_0' class='reply_name'>"+ item.username + "</p>"
					+ "<p id='reply_time"+(start+i)+"_0' class='reply_time'>"+ dynamicDate(itemTime/1000) + "</p>"
					+ "<p id='reply_text"+(start+i)+"_0' class='reply_text'>"+ item.contents + "</p>"
					+ "<input id='postId"+(start+i)+"' style='display:none;' value='"+item.postid+"'>"					
					+ "</li>";
					
		$.each( item.reply, function(j, replyItem){
			itemTime = new Date( dateTimeFormat( replyItem.time ) );

			temp = temp 
				+ "<li id='replyListLi"+(start+i)+"_"+(j+1)+"'class='replyListLi'>"
				+ "<img id='reply_profile_small_frame"+(start+i)+"_"+(j+1)+"' class='reply_profile_small_frame' border='0' src='../images/transparency.png' />"
				+ "<p id='reply_name"+(start+i)+"_"+(j+1)+"' class='reply_name'>"+ replyItem.username + "</p>"
				+ "<p id='reply_time"+(start+i)+"_"+(j+1)+"' class='reply_time'>"+ dynamicDate(itemTime/1000) + "</p>"
				+ "<p id='reply_text"+(start+i)+"_"+(j+1)+"' class='reply_text'>"+ replyItem.contents + "</p>"
				+ "</li>"
		});
		
		temp += "</ul></div>"; 

		// 선택된 포스트이냐, 아님 포스트 리스트이냐
		if( postInfo.selectedPostId != -1 ){
			temp += "</li>"
				+ "<p id='btnAddReply' class='btnAddReply'></p>";
			$("#btn_reload").hide();			
		}
		else{
			temp += "<p id='reply_bg_bottom' class='reply_bg_bottom'></p>"
			+ "</li>";
			$("#btn_reload").show();
		}
		
		$("#postList").append(temp);
		$('#postImage'+(start+i)).css("left", ($(window).width()/2 -138) + "px");
		
		// type별로 아이콘 띄우기
		$("#img_bg"+(start+i)).css("background-image", iconType( item.diary.diarytypeid, item.photo.filename, item.movie.filename ));
		
		// 포토 포스트이면 이미지를 띄우고 아니면 기본 이미지
		if( item.photo.filename != null ) $('#postImage'+(start+i)).css("background-image", urlPhoto( postInfo.babyId, item.photo.filename) );
		else $('#postImage'+(start+i)).css("background-image", "url('../images/mainframe/btn_photo.png')" );
		
		// 프로필 이미지를 어떻게?
		$('#main_profile_small_frame'+(start+i)).css("background-image", "url('../images/mainframe/sum_small_baby.png')"+", "+urlProfilePhoto( postInfo.babyId, postInfo.pf_name, 'small') );

		if( postInfo.selectedPostId != -1 )
		{
			new Quickly('btnAddReply', function(e){
				$.mobile.changePage("sns/reply.html",{transition:'slide'});
			})
		}
		
		var replyBgHeight=0;
		
		// 본문의 height결정
		$('#replyListLi'+(start+i)+"_0").css("height", (39+($("#reply_text"+(start+i)+"_0").css('height').replace('px','')*1))+"px" );
		$('#reply_profile_small_frame'+(start+i)+"_0").css("background-image", urlProfileFrame( 1, 1, item.usergender )+", "+urlProfilePhoto( postInfo.babyId, postInfo.pf_name, 'small') );

		// 본문외의 댓글 height 결정
		replyBgHeight += ($('#replyListLi'+(start+i)+"_0").css('height').replace('px','') * 1 );

		$.each( item.reply, function(j, replyItem){
			$('#replyListLi'+(start+i)+"_"+(j+1)).css("height", (39+($("#reply_text"+(start+i)+"_"+(j+1)).css('height').replace('px','')*1))+"px" );
			replyBgHeight += ($('#replyListLi'+(start+i)+"_"+(j+1)).css('height').replace('px','') * 1 );
			
			$('#reply_profile_small_frame'+(start+i)+"_"+(j+1)).css("background-image", urlProfileFrame( 1, replyItem.usertype, replyItem.usergender )+", "+urlProfilePhoto( replyItem.babyid, replyItem.pf_name, 'small') );
		});

		$('#reply_bg'+(start+i)).css("height",replyBgHeight+"px");

		postInfo.startIndex = i+1;
	});

	// 사진을 클릭했을 때, 그 반응들
	$.each( postData, function(i, item){		
		$("#detailPost"+i).click( function(){
			if( postInfo.selectedPostId == -1 )
			{
				btn = $(this);
				postInfo.selectedPostId = item.postid;
				postInfo.selectedPostUserId = item.userid;
				requestDetailPostToServer( user.id, item.postid, 'babyroom' );
			}
		});
		
		$("#postImage"+(start+i)).click( function(){
			btn = $(this);
			if( $("#imgDetail").css("display") == "none"){
				$("#imgDetail").css("background-color", "#000000");
				$("#imgDetail").css("background-image", "url("+$("#imgUrl"+(btn.attr('id').substr(9,10) * 1)).val()+")");
				$("#imgDetail").css("display", "block");
			}
		});
	})
	
	for (x in myScroll) 
		myScroll[x].refresh(); 
}

function refreshSnsPost( postData, start )
{
	if( start == 0 )
		$("#snsPostList").html("");

	$.each( postData, function(i, item){
		var itemTime = new Date( dateTimeFormat ( item.datetime ) );

		var temp = "<li id='detailSnsPost"+(start+i)+"'>"
					+ "<input id='snsImgUrl"+(start+i)+"' style='display:none;' value='"+serverURL+'/photo/'+item.babyid+'/post_'+item.photo.filename+"'/>"
					+ "<div id='snsImg_bg"+(start+i)+"' class='img_bg'>"
					+ "<img id='main_sns_profile_small_frame"+(start+i)+"' class='main_profile_small_frame' border='0' src='../images/transparency.png'/>"
					+ "<p id='main_sns_title_text' class='main_title_text'>" + item.title + "</p>"
					+ "<p id='main_sns_title_time' class='main_title_time'>" + dynamicDate(itemTime/1000) + "</p>"
					+ "<img id='snsPostImage"+(start+i)+"' class='postImage' border='0' src='../images/transparency.png'/></div>"
					+ "<div id='snsReply_bg"+(start+i)+"' class='reply_bg'>"
					+ "<ul id='snsReplyList"+(start+i)+"' class='replyList'>"
					//내용 부분 추가
					+ "<li id='snsReplyListLi"+(start+i)+"_0' class='replyListLi'>"
					+ "<img id='snsReply_profile_small_frame"+(start+i)+"_0' class='reply_profile_small_frame' border='0' src='../images/transparency.png' />"
					+ "<p id='snsReply_name"+(start+i)+"_0' class='reply_name'>"+ item.username + "</p>"
					+ "<p id='snsReply_time"+(start+i)+"_0' class='reply_time'>"+ dynamicDate(itemTime/1000) + "</p>"
					+ "<p id='snsReply_text"+(start+i)+"_0' class='reply_text'>"+ item.contents + "</p>"
					+ "<input id='snsPostId"+(start+i)+"' style='display:none;' value='"+item.postid+"'>"
					+ "</li>";

		$.each( item.reply, function(j, replyItem){
			itemTime = new Date( dateTimeFormat ( replyItem.time ) );

			temp += "<li id='snsReplyListLi"+(start+i)+"_"+(j+1)+"'class='replyListLi'>"
				+ "<img id='snsReply_profile_small_frame"+(start+i)+"_"+(j+1)+"' class='reply_profile_small_frame' border='0' src='../images/transparency.png' />"
				+ "<p id='snsReply_name"+(start+i)+"_"+(j+1)+"' class='reply_name'>"+ replyItem.username + "</p>"
				+ "<p id='snsReply_time"+(start+i)+"_"+(j+1)+"' class='reply_time'>"+ dynamicDate(itemTime/1000) + "</p>"
				+ "<p id='snsReply_text"+(start+i)+"_"+(j+1)+"' class='reply_text'>"+ replyItem.contents + "</p>"
				+ "</li>";
		});
		
		temp += "</ul></div>";
		
		// 선택된 포스트이냐, 아님 포스트 리스트이냐
		if( snsPostInfo.selectedPostId != -1 ){
			temp += "</li>"
				+ "<p id='snsBtnAddReply' class='btnAddReply'></p>";
			$("#snsBtn_reload").hide();
		}
		else{
			temp += "<p id='snsReply_bg_bottom' class='reply_bg_bottom'></p>"
			+ "</li>";
			$("#snsBtn_reload").show();
		}

		$("#snsPostList").append(temp);
		$('#snsPostImage'+(start+i)).css("left", ($(window).width()/2 -138) + "px");
		
		$("#snsImg_bg"+(start+i)).css("background-image", iconType( item.diary.diarytypeid, item.photo.filename, item.movie.filename ));

		if( item.photo.filename != null ) $('#snsPostImage'+(start+i)).css("background-image", urlPhoto( item.babyid, item.photo.filename) );
		else $('#snsPostImage'+(start+i)).css("background-image", "url('../images/mainframe/btn_photo.png')" );
		
		$('#main_sns_profile_small_frame'+(start+i)).css("background-image", "url('../images/mainframe/sum_small_baby.png')"+", "+urlProfilePhoto( item.babyid, item.pf_name, 'small') );
		
		if( snsPostInfo.selectedPostId != -1 )
		{
			new Quickly('snsBtnAddReply', function(e){
				$.mobile.changePage("sns/reply.html",{transition:'slide'});
			})
		}

		var replyBgHeight=0;
		$('#snsReplyListLi'+(start+i)+"_0").css("height", (39+($("#snsReply_text"+(start+i)+"_0").css('height').replace('px','')*1))+"px" );
		$('#snsReply_profile_small_frame'+(start+i)+"_0").css("background-image", urlProfileFrame( 1, 1, item.usergender )+", "+urlProfilePhoto( item.babyid, item.pf_name, 'small') );
		replyBgHeight += ($('#snsReplyListLi'+(start+i)+"_0").css('height').replace('px','') * 1 );
		
		$.each( item.reply, function(j, replyItem ){
			$('#snsReplyListLi'+(start+i)+"_"+(j+1)).css("height", (39+($("#snsReply_text"+(start+i)+"_"+(j+1)).css('height').replace('px','')*1))+"px" );
			replyBgHeight += ($('#snsReplyListLi'+(start+i)+"_"+(j+1)).css('height').replace('px','') * 1 );

			$('#snsReply_profile_small_frame'+(start+i)+"_"+(j+1)).css("background-image", urlProfileFrame( 1, replyItem.usertype, replyItem.usergender )+", "+urlProfilePhoto( replyItem.babyid, replyItem.pf_name, 'small') );
		});
		
		$('#snsReply_bg'+(start+i)).css("height",replyBgHeight+"px");
		snsPostInfo.startIndex = i+1;
	});

	$.each( postData, function(i, item){
		$("#detailSnsPost"+(start+i)).click( function(){
			if( snsPostInfo.selectedPostId == -1 )
			{
				btn = $(this);
				snsPostInfo.selectedPostId = item.postid;
				snsPostInfo.selectedPostUserId = item.userid;

				requestDetailPostToServer( user.id, item.postid, 'sns' );
			}
		});
		$("#snsPostImage"+(start+i)).click( function(){
			btn = $(this);
			if( $("#imgDetail").css("display") == "none"){
				$("#imgDetail").css("background-color", "#000000");
				$("#imgDetail").css("background-image", "url("+$("#snsImgUrl"+(btn.attr('id').substr(12,10) * 1)).val()+")");
				$("#imgDetail").css("display", "block");
			}
		});
	})

	for (x in myScroll) 
		myScroll[x].refresh(); 
}

// 수정 완료
function refreshProfile( result, profileBabyId ){
	profileInfo.babyId = profileBabyId;
	profileInfo.babyInfo = result.babyinfo;
	profileInfo.growth = result.growth;
	profileInfo.babyNick = profileInfo.babyInfo.nick;

	$("#baby_profile").css("background-image", "url(../images/profile/profile_frame_big.png), " + urlProfilePhoto(profileInfo.babyId, profileInfo.babyInfo.pf_name, 'large') );
		
	$("#profile_gender").attr("src",checkGender(1, profileInfo.babyInfo.gender));
	$("#profile_babynick").html(profileInfo.babyNick);
	$("#profile_babyname").html(profileInfo.babyInfo.name);
	$("#profile_babybirthday").html("생일 "+profileInfo.babyInfo.birthday);
	$("#profile_babyfrombirth").html("태어난지 "+ countDay(profileInfo.babyInfo.birthday) +"일 되는 날");

	$.each(profileInfo.growth, function(i, item){
		$("#profile_btn_growth"+(item*1-7)).css("background-image", "url(../images/profile/badge_on.png)");
	});
	
	switch( result.profile_result.relation )
	{
		case '1':	// 부모면
			profileMyBaby( result );
			break;
		case '2':
		case '3':	// 가족, 친구면
			profileFriendsBaby( result );
			break;
		case '4':	// 친구가 아니면
			profileNotFriendBaby( result );
			break;
	}
}

//수정 완료
function profileMyBaby( result )
{
	$("#header").append("<p id='btnHeaderModify' class='btnHeaderModify btnHeaderRight'></p>");
	new Quickly('btnHeaderModify', function(e){
		$.mobile.changePage("profile/modify_profile.html", {transition : 'slide'});
	})
			
	$("#profile_number_friends").html(profileInfo.babyInfo.num_of_friend);
	$("#profile_number_family").html(result.profile_result.num_of_family);
	$("#profile_number_mybaby").html(result.profile_result.num_of_baby);
		
	new Quickly('profile_friends', function(e){
		$.mobile.changePage("profile/friendlist.html", {transition : 'slide'});
	});
	new Quickly('profile_family', function(e){
		$.mobile.changePage("profile/familylist.html", {transition : 'slide'});
	});
	new Quickly('profile_mybaby', function(e){
		$.mobile.changePage("profile/agalist.html", {transition : 'slide'});
	})
			
	$("#btnInviteFriend").css("background-image", "url(../images/profile/btn_addroom.png)");
	new Quickly('btnInviteFriend', function(e){
		$.mobile.changePage("profile/create_room.html", {transition : 'none'});
	});

	new Quickly('btnMessage', function(e){
		$.mobile.changePage("sns/messagebox.html", {transition : 'slide'});
	})
			
	user.info.num_of_baby = result.profile_result.num_of_baby;
	user.info.num_of_family = result.profile_result.num_of_family;
	user.info.num_of_friend = profileInfo.babyInfo.num_of_friend;
	localStorage.setItem('userInfo', JSON.stringify(user.info) );
}

function profileFriendsBaby( result )
{
	$("#profile_family").hide();
	$("#profile_mybaby").hide();
	$("#profile_number_family").hide();
	$("#profile_number_mybaby").hide();
	$("#profile_number_friends").html(profileInfo.babyInfo.num_of_friend);

	$("#btnInviteFriend").css("background-image", "url(../images/profile/btn_friendroom.png)");
	new Quickly('btnInviteFriend', function(e){
		postInfo.init();
		postInfo.visitFriend = true;
		postInfo.babyId = profileInfo.babyId;
		postInfo.babyNick = profileInfo.babyInfo.nick;
		postInfo.pf_name = profileInfo.babyInfo.pf_name;
		postInfo.babyInfo = profileInfo.babyInfo;
		$.mobile.changePage("main_aga.html", {transition:'slide'});
	});
	
	$("#btnMessage").css("background-image", "url(../images/profile/btn_sendmessage.png)");
	new Quickly('btnMessage', function(e){
		messageInfo.babyId = profileInfo.babyId;
		messageInfo.select = true;
		$.mobile.changePage("sns/sendMessage.html", {transition:'slide'});
	});
}

function profileNotFriendBaby( result )
{
	$("#btnMessage").hide();
	$("#profile_family").hide();
	$("#profile_mybaby").hide();
	$("#profile_number_family").hide();
	$("#profile_number_mybaby").hide();
	$("#profile_number_friends").html(profileInfo.babyInfo.num_of_friend);

	$("#btnInviteFriend").css("background-image", "url(../images/profile/btn_addfriend.png)");
	new Quickly('btnInviteFriend', function(e){
		requestFriendToServer( user.mainbaby_info.babyid, profileInfo.babyId, 0 );
	})
}

$(document).ready(
	function(){
		if (navigator.userAgent.indexOf("Android") != -1)
		{
			$.mobile.defaultPageTransition = 'none';
			$.mobile.defaultDialogTransition = 'none';
		}		
		
		new Quickly('tab1', function(e){
			postInfo.init();
			btnClear(); $("#tab1").attr({src:"../images/tab/tab_1_on.png"});
			$.mobile.changePage("main_aga.html");
		});
		new Quickly('tab2', function(e){
			snsPostInfo.init();
			btnClear(); $("#tab2").attr({src:"../images/tab/tab_2_on.png"});
			$.mobile.changePage("main_sns.html");
		});
		new Quickly('tab3', function(e){
			btnClear(); $("#tab3").attr({src:"../images/tab/tab_3_on.png"});
			$.mobile.changePage("main_diary.html");
		});
		new Quickly('tab4', function(e){
			profileInfo.init();
			btnClear(); $("#tab4").attr({src:"../images/tab/tab_4_on.png"}); 
			$.mobile.changePage("main_profile.html");
		});
		new Quickly('tab5', function(e){
			btnClear(); $("#tab5").attr({src:"../images/tab/tab_5_on.png"});
			$.mobile.changePage("main_more.html");
		});
	}
)

document.addEventListener('touchmove', function (e) { e.preventDefault(); }, false);

/*
				$(this).simpledialog({
					'mode' : 'bool',
					'prompt' : 'How about it?',
					'useModal': true,
					'buttons' : {
					  'OK': {
						click: function () {
						 
						}
					  },
					  'Cancel': {
						click: function () {
						  $('#dialogoutput').text('Cancel');
						},
						icon: "delete",
						theme: "c"
					  }
					}
				  })
*/	